﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
/// Summary description for SQLConnect
/// </summary>
public class SQLConnect
{
    private static SqlConnection conn;
    protected static String strConnect = ConfigurationManager.ConnectionStrings["ql_ThongTinNhaTro"].ConnectionString;
    public static String ConnnectionString
    {
        get
        {
            return strConnect;
        }
    }
    public SQLConnect()
    {
        conn = new SqlConnection(strConnect);
    }
    public DataTable ExecuteQuery(String sql, CommandType commandType, List<SqlParameter> pars)
    {
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            SqlCommand com = new SqlCommand(sql, conn);
            com.CommandType = commandType;
            if (pars != null)
            {
                for (int i = 0; i < pars.Count; i++)
                {
                    com.Parameters.Add(pars[i]);
                }
            }
            SqlDataAdapter dad = new SqlDataAdapter(com);
            dad.Fill(ds);
            conn.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return ds.Tables[0];
    }
    public DataTable ExecuteQuery(String sql, CommandType commandType)
    {
        return ExecuteQuery(sql, commandType, null);
    }
    public int ExecuteNonQuery(String sql, CommandType commandType, List<SqlParameter> pars)
    {
        int n = -1;
        try
        {
            conn.Open();
            SqlCommand com = new SqlCommand(sql, conn);
            com.CommandType = commandType;
            for (int i = 0; i < pars.Count; i++)
            {
                com.Parameters.Add(pars[i+1]);
            }
            n = com.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
        }
        return n;
    }
}
